package com.atguigu.boot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author lfy
 * @Description
 * @create 2023-03-27 18:27
 */
//@ResponseBody
//@Controller
@RestController
@CrossOrigin
public class HelloController {


    @Autowired
    StringRedisTemplate redisTemplate;

    @PostMapping("/upload")
    public ResponseEntity<String> uploadFile(@RequestParam("img") MultipartFile file){
        String filePath = "/static/img";
        String fileName = file.getOriginalFilename();
        LocalDateTime now = LocalDateTime.now();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String formattedNow = now.format(formatter);
        System.out.println(formattedNow);
        assert fileName != null;
        File dest = new File(filePath + formattedNow + '.' + fileName);

        try {
            file.transferTo(dest);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return new ResponseEntity<>("File uploaded succesfully", HttpStatusCode.valueOf(200));
    }


    @GetMapping("/hello")
    public String hello(){

        return "Hello,Spring Boot 3!";
    }


    @GetMapping("/incr")
    public String incr(){
        Long haha = redisTemplate.opsForValue().increment("haha");
        return "增加后的值："+haha;
    }

}
